﻿using System;
using System.Collections.Generic;
using System.Xml;
using JUtil.Extensions;

namespace JUtil.CodeGenerator
{
    class Configuration : IBuildXmlElement
    {
        public void Explore()
        {
            JUtil.Path.File.Explore(filepath);
        }

        public static void Reset()
        {
            _Instance = null;
        }

        private static Configuration _Instance;
        public static Configuration Instance
        {
            get
            {
                if (_Instance == null)
                {
                    string filepath = JUtil.Path.File.GetAbsolutePath(JUtil.Path.Directory.Application, "ProjectDataGG.xml");

                    _Instance = new Configuration(filepath);
                }
                return _Instance;
            }
        }

        public List<string> ProjectNames
        {
            get
            {
                List<string> lst = new List<string>();

                foreach (ProjectConfig config in ProjectConfigs)
                    lst.Add(config.ProjectName);

                return lst;
            }
        }

        private Configuration(string filepath)
        {

            this.filepath = filepath;

            this.ProjectConfigs = new List<ProjectConfig>();

            Load();
        }

        ~Configuration()
        {
            try
            {
                Save();
            }
            catch (Exception ex)
            {
            	Log.ReportError(ex);
            }
        }

        public void UpdateProject(ProjectConfig config)
        {
            ProjectConfig match = GetProject(config.ProjectName);

            if (match != null)
                match.Update(config);
            else
                ProjectConfigs.Add(config);
        }

        public ProjectConfig GetProject(string ProjectName)
        {
            return ProjectConfigs.Find((c) => { return ProjectName == c.ProjectName; });
        }

        private void Load()
        {
            if (!JUtil.Path.File.Exists(filepath)) return;

            XmlDocument doc = new XmlDocument();

            doc.Load(filepath);

            XmlElement root = doc.DocumentElement;

            ActiveProjectName = Convert.ToString(root.Attributes["ActiveProjectName"].Value);

            foreach (XmlElement el in root.SelectNodes("//Configuration//ProjectConfig"))
            {
                //
                // 目前不知道 SelectNodes 找只屬於currentNode的子節點，因此加入以下
                //
                if (el.ParentNode != root) continue;

                ProjectConfig config = new ProjectConfig(el);

                ProjectConfigs.Add(config);
            }
        }

        public void Save()
        {
            XmlDocument doc = new XmlDocument();

            XmlElement el = BuildXmlElement(doc);

            doc.AppendChild(el);

            doc.SaveAs("big5", filepath);
        }

        public XmlElement BuildXmlElement(XmlDocument doc)
        {
            XmlElement el = doc.CreateElement("Configuration");

            el.SetAttribute("ActiveProjectName", ActiveProjectName);

            foreach (ProjectConfig config in ProjectConfigs)
            {
                if (config is IBuildXmlElement)
                {
                    XmlElement child = (config as IBuildXmlElement).BuildXmlElement(doc);

                    el.AppendChild(child);
                }
            }

            return el;
        }

        private readonly string filepath;

        public string ActiveProjectName;

        private List<ProjectConfig> ProjectConfigs;
    }
    
}
